package com.xuyanze.es.enums;

import com.xuyanze.es.face.ISegment;
import com.xuyanze.es.model.EsSegment;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;


/**
 * @author ：lzy
 * @date ：2023-01-13
 * @description：
 */
public enum EsKeyWord implements ISegment {

    // A OR B
    OR_JOIN,

    EQ{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return QueryBuilders.termQuery(segment.getField(), segment.getVal());
        }
    },

    EQ_TERMS{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return QueryBuilders.termsQuery(segment.getField(),segment.getVals());
        }
    },

    GT{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return QueryBuilders.rangeQuery(segment.getField()).gt(segment.getVal());
        }
    },

    GE{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return QueryBuilders.rangeQuery(segment.getField()).gte(segment.getVal());
        }
    },

    LT{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return QueryBuilders.rangeQuery(segment.getField()).lt(segment.getVal());
        }
    },

    LE{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return QueryBuilders.rangeQuery(segment.getField()).lte(segment.getVal());
        }
    },

    LIKE{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return QueryBuilders.wildcardQuery(segment.getField(),String.format("*%s*", segment.getVal()));
        }
    },

    LIKE_LEFT{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return QueryBuilders.wildcardQuery(segment.getField(),String.format("%s*", segment.getVal()));
        }
    },

    LIKE_RIGHT{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return QueryBuilders.wildcardQuery(segment.getField(),String.format("*%s", segment.getVal()));
        }
    },

    BETWEEN{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return QueryBuilders.rangeQuery(segment.getField()).from(segment.getFrom()).to(segment.getTo());
        }
    },

    EXISTS{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return QueryBuilders.existsQuery(segment.getField());
        }
    },

    IN{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return QueryBuilders.termsQuery(segment.getField(),segment.getVal());
        }
    },

    CHILDREN_QUERY{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return segment.getChildrenQueryBuilder();
        }
    },

    MATCH_ALL_QUERY{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return QueryBuilders.matchAllQuery();
        }
    },

    MATCH_PHRASE_QUERY{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return QueryBuilders.matchPhraseQuery(segment.getField(),segment.getVal()).boost(segment.getBoost());
        }
    },

    MATCH_QUERY{
        @Override
        public QueryBuilder bindQuery(EsSegment segment) {
            return QueryBuilders.matchQuery(segment.getField(),segment.getVal()).boost(segment.getBoost());
        }
    },

    ;


    public QueryBuilder bindQuery(EsSegment segment){
        return null;
    }



}
